package sparqlclient;

import java.util.Map;

public class SparqlClientExample {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        SparqlClient sparqlClient = new SparqlClient("localhost:8080/space");

        String query = "ASK WHERE { ?s ?p ?o }";
        boolean serverIsUp = sparqlClient.ask(query);
        if(serverIsUp){
        	countLacInPyrenee(sparqlClient);
        }
//        if (serverIsUp) {
//            System.out.println("server is UP");
//
//            nbPersonnesParPiece(sparqlClient);
//
//            System.out.println("ajout d'une personne dans le bureau:");
//            query = "PREFIX : <http://www.lamaisondumeurtre.fr#>\n"
//                    + "PREFIX instances: <http://www.lamaisondumeurtre.fr/instances#>\n"
//                    + "INSERT DATA\n"
//                    + "{\n"
//                    + "  instances:Bob :personneDansPiece instances:Bureau.\n"
//                    + "}\n";
//            sparqlClient.update(query);
//
//            nbPersonnesParPiece(sparqlClient);
//
//            System.out.println("suppression d'une personne du bureau:");
//            query = "PREFIX : <http://www.lamaisondumeurtre.fr#>\n"
//                    + "PREFIX instances: <http://www.lamaisondumeurtre.fr/instances#>\n"
//                    + "DELETE DATA\n"
//                    + "{\n"
//                    + "  instances:Bob :personneDansPiece instances:Bureau.\n"
//                    + "}\n";
//            sparqlClient.update(query);
//            
//            nbPersonnesParPiece(sparqlClient);
//            
//        } else {
//            System.out.println("service is DOWN");
//        }
    }
    
    private static void countLacInPyrenee(SparqlClient sparqlClient){
    	String query = "PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>"
                + "PREFIX l:<http://www.semanticweb.org/clement/ontologies/2014/0/Ontologie-Balades#>"
                + "\nSELECT ?other WHERE\n"
                    + "{\n"
                    + "?x rdfs:label 'Balade'."
                    + "OPTIONAL{?x rdfs:label ?other}. "
                    + "}\n";
            Iterable<Map<String, String>> results = sparqlClient.select(query);
            for (Map<String, String> result : results) {
                System.out.println(result.get("other"));
            }
     
    }
    
    private static void nbPersonnesParPiece(SparqlClient sparqlClient) {
        String query = "PREFIX : <http://www.lamaisondumeurtre.fr#>\n"
                    + "SELECT ?piece (COUNT(?personne) AS ?nbPers) WHERE\n"
                    + "{\n"
                    + "    ?personne :personneDansPiece ?piece.\n"
                    + "}\n";
        
        	System.out.println("Requete a executer "+query);
            Iterable<Map<String, String>> results = sparqlClient.select(query);
            System.out.println("requete ");
            for (Map<String, String> result : results) {
                System.out.println(result.get("piece") + " : " + result.get("nbPers"));
            }
    }    
}
